const { CommandInteraction, MessageEmbed } = require('discord.js');

module.exports = {
  name: 'music',
  description: 'Complete music system',
  permission: 'ADMINISTRATOR',
  options: [
    {
      name: 'play',
      description: 'Play a song',
      type: 'SUB_COMMAND',
      options: [
        {
          name: 'query',
          description: 'Provide a name or a url for the song',
          type: 'STRING',
          required: true,
        },
      ],
    },
    {
      name: 'volume',
      description: 'Alter the volume',
      type: 'SUB_COMMAND',
      options: [
        {
          name: 'percent',
          description: '10 - 10%',
          type: 'NUMBER',
          required: true,
        },
      ],
    },
    {
      name: 'settings',
      description: 'Select an option',
      type: 'SUB_COMMAND',
      options: [
        {
          name: 'options',
          description: 'Select an option',
          type: 'STRING',
          required: true,
          choices: [
            { name: '🔢  View Queue', value: 'queue' },
            { name: '⏭ Skip Song', value: 'skip' },
            { name: '⏯ Pause Song', value: 'pause' },
            { name: '⏯ Resume Song', value: 'resume' },
            { name: '⏹ Stop Music', value: 'stop' },
            { name: '🔀 Shuffle Queue', value: 'shuffle' },
            { name: '🔃 Toggle Autoplay Mode', value: 'autoPlay' },
            { name: ' Add a Realted Song', value: 'relatedSong' },
            { name: '🔁 Toggle Repeat Mode', value: 'repeatMode' },
          ],
        },
      ],
    },
  ],
  /**
   *
   * @param {CommandInteraction} interaction
   * @param {Client} client
   */
  async execute(interaction, client) {
    const { options, member, guild, channel } = interaction;
    const voiceChannel = member.voice.channel;

    if (!voiceChannel)
      return interaction.reply({
        content:
          'You must be in a voice channel to be able to use the music command',
        ephemeral: true,
      });

    if (
      guild.me.voice.channelId &&
      voiceChannel.id !== guild.me.voice.channelId
    )
      return interaction.reply({
        content: `I'm already playing music in <#${guild.me.voice.channelId}>.`,
        ephemeral: true,
      });

    try {
      switch (options.getSubcommand()) {
        case 'play':
          client.distube.play(voiceChannel, options.getString('query'), {
            textChannel: channel,
            member: member,
          });
          return interaction.reply({ content: '🎵 Request recieved.' });
        case 'volume':
          const volume = options.getNumber('percent');

          if (volume > 100 || volume < 1)
            return interaction.reply({
              content: 'You have to specify a number between 1 and 100',
            });

          client.distube.setVolume(voiceChannel, volume);
          return interaction.reply({
            content: `↕ Volume has ben set to \`${volume}%\``,
          });
        case 'settings': {
          const queue = await client.distube.getQueue(voiceChannel);

          if (!queue)
            return interaction.reply({ content: `⛔ There is no queue.` });

          switch (options.getString('options')) {
            case 'skip':
              await queue.skip(voiceChannel);
              return interaction.reply({ content: `⏭ Song has been skipped.` });

            case 'stop':
              await queue.stop(voiceChannel);
              return interaction.reply({ content: `⏹ Song has been stopped.` });

            case 'pause':
              await queue.pause(voiceChannel);
              return interaction.reply({ content: `⏸ Song has been paused.` });

            case 'resume':
              await queue.resume(voiceChannel);
              return interaction.reply({
                content: `▶️ Song has been resumed.`,
              });

            case 'shuffle':
              await queue.shuffle(voiceChannel);
              return interaction.reply({
                content: `🔀 Song has been shuffled.`,
              });

            case 'autoPlay':
              let modeAuto = await queue.toggleAutoplay(voiceChannel);
              return interaction.reply({
                content: `🔃 Autoplay Mode is set to ${
                  modeAuto ? 'On' : 'Off'
                }`,
              });

            case 'relatedSong':
              await queue.addRelatedSong(voiceChannel);
              return interaction.reply({
                content: `Related song has been added to the queue`,
              });

            case 'repeatMode':
              let modeRepeat = await client.distube.setRepeatMode(queue);
              return interaction.reply({
                content: `🔃 Replay Mode is set to ${(modeRepeat = modeRepeat
                  ? modeRepeat == 2
                    ? 'Queue'
                    : 'Song'
                  : 'Off')}`,
              });

            case 'queue':
              return interaction.reply({
                embeds: [
                  new MessageEmbed()
                    .setColor('PURPLE')
                    .setDescription(
                      `${queue.songs.map(
                        (song, id) =>
                          `\n**${id + 1}**. ${song.name} - \`${
                            song.formattedDuration
                          }\``
                      )}`
                    ),
                ],
              });
          }
          return;
        }
      }
    } catch (e) {
      const errorEmbed = new MessageEmbed()
        .setColor('RED')
        .setDescription(`⛔ Alert: ${e}`);
      return interaction.reply({ embeds: [errorEmbed] });
    }
  },
};
